//
//  ConfirmOrderViewController.m
//  NewPro
//
//  Created by RYX on 2019/3/28.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "ConfirmOrderViewController.h"
#import "AddAddressStore.h"
#import "AddressModel.h"
#import "PosApplyList.h"
#import "PosApplyModel.h"
#import "AddressViewController.h"
#import "SureOrderList.h"
#import "OrdertoSureVC.h"
#import "addOrderStore.h"
#import "MyRedenvelopesStore.h"
#import "UseRedenvelopesViewController.h"
#import "MyRedenvelopesList.h"
#import "MyRedenvelopesModel.h"
#import "TicketModel.h"
#import "UserShopTicketsModel.h"

@interface ConfirmOrderViewController ()
{
    
    __weak IBOutlet UILabel *nameLa;
    
    __weak IBOutlet UILabel *locationLa;
    __weak IBOutlet UILabel *phoneLa;
    
    __weak IBOutlet UIImageView *im;
    
    __weak IBOutlet UILabel *proName;
    
    __weak IBOutlet UILabel *proPrice;
    
    __weak IBOutlet UILabel *AllCountla;
    
    __weak IBOutlet UILabel *totalMoney;
    
    __weak IBOutlet UILabel *PLabel;
    
    __weak IBOutlet UIView *redPgView;
    __weak IBOutlet UILabel *redBgNumsLa;
    
    CGFloat totalPrice;
}

@property (copy, nonatomic) NSArray *listArr;

@end

@implementation ConfirmOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"确认订单"];
        
    [self getDefaultAddress];
    
    [self setUI];
    
    if (self.PType.length <= 0) {
        
        [self getUserRedBgNum];
    }
}

- (void)setUI{
    
    PosApplyModel *model = [PosApplyList sharedInstance].selectedPosModel;
    [im sd_setImageWithURL:[NSURL URLWithString:model.imgPath]];
    proName.text = model.sellName;
    proPrice.text = [NSString stringWithFormat:@"¥ %.2f",[self.AllPrice floatValue]];
    
    totalPrice = [self.AllPrice floatValue];
    totalMoney.text = [NSString stringWithFormat:@"¥ %.2f",[self.AllPrice floatValue]];
    if (self.PType.length > 0) {
        
        PLabel.text = self.PType;
    }else{
        
        PLabel.text = @"快递 免邮";
    }
    
    AllCountla.text = self.Allcount;
}
- (void)getDefaultAddress{
    
    AddAddressStore *store = [[AddAddressStore alloc] init];
    [store getAllAddressWithSuccess:^(NSArray * _Nonnull listArr) {
       
        for (AddressModel *model in listArr) {
            
            if ([model.isDefault isEqualToString:@"1"]) {
            
                [SureOrderList sharedInstance].selectedAdModel = model;
                self->nameLa.text = [NSString stringWithFormat:@"收货人：%@",model.name];
                self->locationLa.text = [NSString stringWithFormat:@"收货地址：%@",model.address];
                self->phoneLa.text = model.phone;
                return ;
            }
        }
        
    } Failure:^(NSError * _Nonnull error) {
        
    }];
}

- (IBAction)onPostBtn:(UIButton *)sender {
    PosApplyModel *model = [PosApplyList sharedInstance].selectedPosModel;
    if (locationLa.text <= 0 || [locationLa.text isEqualToString:@"收货地址："]) {
        return [self showMBPError:@"请选择收货地址"];
    }
    NSString *totalStr = [NSString stringWithFormat:@"%.2f",totalPrice];
    NSString *uid = model.uid;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:uid forKey:@"modelId"];
    [dic setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"customId"];
    [dic setObject:[SureOrderList sharedInstance].selectedAdModel.address forKey:@"address"];
    [dic setObject:[SureOrderList sharedInstance].selectedAdModel.uid forKey:@"addressId"];
    [dic setObject:self.Allcount forKey:@"count"];
    
    if ([MyRedenvelopesList sharedInstance].selectedRedenvelopes == nil || [[MyRedenvelopesList sharedInstance].selectedRedenvelopes.uid isEqualToString:@"-1"]) {
        
        NSArray *arr = [NSArray array];
        [dic setObject:arr forKey:@"ticketIds"];
        
    }else{
        
        NSArray *arr = [NSArray arrayWithObject:[MyRedenvelopesList sharedInstance].selectedRedenvelopes.uid];
        [dic setObject:arr forKey:@"ticketIds"];
    }
    
    [[[addOrderStore alloc]init] addOrderWithPrams:dic AndType:self.VCType Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            
            [MyRedenvelopesList sharedInstance].selectedRedenvelopes = nil;
            //刷新页面数据
            [self getUserRedBgNum];
            
            OrdertoSureVC *VC = [[OrdertoSureVC alloc]init];
            VC.tag = @"机具申领";
            VC.price = totalStr;
            VC.OrderID = [NSString stringWithFormat:@"%@",res[@"data"][@"id"]];
            [self.navigationController pushViewController:VC animated:YES];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (IBAction)onAddressBtn:(UIButton *)sender {
    AddressViewController *VC = [[AddressViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)setModel:(AddressModel *)model{
    
    [SureOrderList sharedInstance].selectedAdModel = model;
    NSString *name = @"收货人：";
    NSString *location = @"收货地址：";
    NSString *phone = @"";
    if (model.uid.length > 0) {
        name = [NSString stringWithFormat:@"收货人：%@",model.name];
        location = [NSString stringWithFormat:@"收货地址：%@",model.address];
        phone = model.phone;
    }
    nameLa.text = name;
    locationLa.text = location;
    phoneLa.text = phone;
}

#pragma mark - 获取可用红包
- (void)getUserRedBgNum{
    
    MJWeakSelf
    MyRedenvelopesStore *store = [[MyRedenvelopesStore alloc] init];
    [store getModelUserRedBgWithModelId:[PosApplyList sharedInstance].selectedPosModel.uid Success:^(NSArray *arr, NSInteger totalCount) {
        
        if (arr.count > 0) {
            
            self->redPgView.alpha = 1;
            self->redBgNumsLa.text = [NSString stringWithFormat:@"%ld个可用",(long)totalCount];
            weakSelf.listArr = arr;
            self->redBgNumsLa.textColor = UIColor.redColor;
            
        }else{
            
            self->redPgView.alpha = 0;
        }
        
    } Failure:^(NSError *error) {
        
        self->redPgView.alpha = 0;
        [self showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - 点击红包
- (IBAction)onRedPgBtn:(UIButton *)sender {
    
    if ([redBgNumsLa.text isEqualToString:@"暂无可用"]) {
        return;
    }
    
    UseRedenvelopesViewController *vc = [[UseRedenvelopesViewController alloc] init];
    vc.ticketId = @"1";
    vc.selectedRedBgBlock = ^{
      
        if ([[MyRedenvelopesList sharedInstance].selectedRedenvelopes.uid isEqualToString: @"-1"]) {
            
            self->totalPrice = [self.AllPrice floatValue];
            self->totalMoney.text = [NSString stringWithFormat:@"¥ %.2f",[self.AllPrice floatValue]];
            self->redBgNumsLa.text = @"不使用红包";
            self->redBgNumsLa.textColor = [UIColor colorWithHexString:@"424242"];
            
        }else{
            
            self->totalPrice = [self.AllPrice floatValue] - [[MyRedenvelopesList sharedInstance].selectedRedenvelopes.amount intValue];
            self->totalMoney.text = [NSString stringWithFormat:@"¥ %.2f",[self.AllPrice floatValue] - [[MyRedenvelopesList sharedInstance].selectedRedenvelopes.amount intValue]];
            self->redBgNumsLa.textColor = UIColor.redColor;
            self->redBgNumsLa.text = [NSString stringWithFormat:@"-￥%@",[MyRedenvelopesList sharedInstance].selectedRedenvelopes.amount];
        }
    };
    [self.navigationController pushViewController:vc animated:YES];
}
@end
